<template>
  <px-dialog v-model="isVisible" class="equip-dialog" title="选择保养项目" width="1200px" @open="openDialog" @close="closeDialog">
    <div class="equip-wrap-common">
      <px-form class="custom-form-ui equip-form" :model="queryParams" label-position="right" label-width="120px">
        <px-row :gutter="10" style="width: 100%">
          <px-col :span="10">
            <px-form-item label="保养项目名称">
              <px-input
                v-model="queryParams.name"
                placeholder="请输入保养项目名称"
                maxlength="50"
                @blur="queryParams.name = $event.target.value.trim()"
              />
            </px-form-item>
          </px-col>
          <px-col :span="4">
            <px-button type="primary" @click="queryTable">查询</px-button>
            <px-button @click="resetTable">重置</px-button>
          </px-col>
        </px-row>
      </px-form>
      <px-table :data="tableData" class="equip-table" size="small" max-height="340px" border @selection-change="selectionChange">
        <px-table-column type="selection" min-width="40" align="center" />
        <!-- <px-table-column label="序号" width="50" /> -->
        <px-table-column prop="name" label="保养项目名称" min-width="220" show-overflow-tooltip />
        <px-table-column prop="standard" label="保养标准" min-width="350" show-overflow-tooltip>
          <template #default="scope">
            <span v-for="(item, index) in scope.row.attachmentList" :key="item.id">
              {{ item.originalName }}{{ scope.row.attachmentList?.length > 1 && index !== scope.row.attachmentList?.length - 1 ? "，" : "" }}
            </span>
          </template>
        </px-table-column>
        <px-table-column prop="confirmWayName" label="确认方式" min-width="150" show-overflow-tooltip />
        <px-table-column prop="createByName" label="创建人" min-width="150" show-overflow-tooltip />
        <px-table-column prop="createTime" label="创建时间" min-width="200" show-overflow-tooltip />
      </px-table>
      <pagination
        v-show="total > 0"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        class="tool-pagination"
        :total="total"
        @pagination="queryTable"
      />
    </div>
    <div style="margin-top: 16px; text-align: right">
      <px-button @click="closeDialog">取消</px-button>
      <px-button type="primary" @click="handleSubmit">确定</px-button>
    </div>
  </px-dialog>
</template>
<script setup lang="ts">
import { queryItemsList } from "@/api/erc/epm";
import { computed, reactive, toRefs } from "vue";

// 调用父级-关闭弹窗方法
const emits = defineEmits(["closeDialog"]);
// 父级传过来的值
const props = defineProps(["visible", "typeCode"]);

const isVisible = computed(() => props.visible);

const state = reactive<any>({
  queryParams: {
    pageNum: 1,
    pageSize: 20
  },
  tableData: [],
  total: 0,
  selectedData: null
});
const { queryParams, tableData, total, selectedData } = toRefs(state);

// 表格多选框选中
const selectionChange = (val: any) => {
  selectedData.value = val;
};

// 查询工具列表
const queryTable = async () => {
  const data = await queryItemsList({ ...queryParams.value, typeCode: props.typeCode });
  tableData.value = data?.data || [];
  total.value = data?.total || 0;
};
// 重置筛选条件和工具列表
const resetTable = async () => {
  queryParams.value = {
    pageNum: queryParams.value.pageNum,
    pageSize: queryParams.value.pageSize
  };
  queryTable();
};
// 打开弹窗
const openDialog = () => {
  queryTable();
};
// 查询列表关闭弹窗
const closeDialog = () => {
  emits("closeDialog");
};

const handleSubmit = () => {
  emits("closeDialog", selectedData.value);
};
</script>

<style lang="scss" scoped>
@import url("@/views/erc/scss/table-list.scss");

.equip-wrap-common {
  height: auto;
}
</style>
